<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>登录</h1>
      <el-card class="login-card" shadow="never">
        <!--登录表单-->
        <el-form ref="form" :model="loginForm" :rules="loginRules">
          <!--用户手机号-->
          <el-form-item prop="mobile">
            <el-input v-model="loginForm.mobile" maxlength="11" placeholder="请输入手机号" />
          </el-form-item>
          <!--用户密码-->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入密码" show-password />
          </el-form-item>
          <!--勾选框-->
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree" label="用户平台使用协议" name="type" />
          </el-form-item>
          <!--登录按钮-->
          <el-form-item>
            <el-button type="primary" @click="loginHandler">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        mobile: process.env.NODE_ENV === 'development' ? '13800000002' : '',
        password: process.env.NODE_ENV === 'development' ? `itHeiMa@${new Date().toISOString().slice(0, 10).replace(/-/g, '')}` : '',
        isAgree: process.env.NODE_ENV === 'development'
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '登录密码必须为6-16位之间的数字,英文', trigger: 'blur' }
        ],
        isAgree: [{
          /**
           * 使用自定义校验规则
           * @param rule 校验规则
           * @param value 校验的值
           * @param callback 回调函数
           */
          validator: (rule, value, callback) => {
            value ? callback() : callback(new Error('请勾选用户产品协议'))
          }
        }]
      }
    }
  },
  methods: {
    // 登录事件
    loginHandler() {
      // 进行整体表单校验
      this.$refs.form.validate(async(isOk) => {
        if (isOk) {
          // $store.dispatch('模块名/数据变量名',额外参数)
          // Vuex的actions 返回的是一个promise，所以需要添加async await
          await this.$store.dispatch('user/login', this.loginForm)
          // 跳转到主页
          this.$message.success('欢迎登录人力资源管理后台！')
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;

  .logo {
    width: 60%;
    height: 100%;
    background: rgba(38, 72, 176) url("../../assets/common/login_back.png") no-repeat center / cover;
    border-top-right-radius: 50px;
    border-bottom-right-radius: 50px;
    border-bottom-left-radius: 30px;
  }

  .form {
    width: 40%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .el-card {
      border: 0;
      padding: 0;
    }

    h1 {
      font-size: 25px;
    }

    .el-input {
      width: 350px;
      height: 50px;

      input {
        width: 100%;
        height: 100%;
      }

      .el-input__inner {
        background-color: #f4f5fb;
      }
    }

    .el-checkbox {
      color: #606266;
    }

    .el-button {
      width: 350px;
    }
  }
}

</style>
